<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="./script/rem.js"></script>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/aui-tab.js" ></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>

    <header class="head" style="padding-top:0.4rem;">
        <div class="head-l" tapmode onclick="closeWin();"><i class="fa fa-angle-left"></i></div>
        <h3 id="title">全部订单</h3>
        <div class="head-r"></div>
    </header>
    <div class="head-d" style="padding-top:0.4rem;"></div>
    <div id="list"></div>
    <!-- 商品 -->
<!--     <div class="shop-order-goods mt20">
        <h3>订单号：21465748484 <span>待收货</span></h3>
        <ul tapmode onclick="order_details()">
            <li>
                <img src="image/classify.jpg">
                <dl>
                    <dt>澳大利亚·毛孔脏东西浮出来·麦卢...</dt>
                    <p>规格：套装</p>
                    <dd>
                        ￥166.00
                        <span>x3</span>
                    </dd>
                </dl>
            </li>
            <li>
                <img src="image/classify.jpg">
                <dl>
                    <dt>澳大利亚·毛孔脏东西浮出来·麦卢...</dt>
                    <p>规格：套装</p>
                    <dd>
                        ￥166.00
                        <span>x3</span>
                    </dd>
                </dl>
            </li>
        </ul>
        <div class="shop-order-goods-m">
            <span>共20件</span>
            合计￥2301.00
        </div>
        <div class="shop-order-goods-d">
            <span>取消订单</span>
            <span>查看物流</span>
            <span class="on">确认收货</span>
        </div>
    </div> -->


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/index.js"></script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="shop-order-goods mt20">
<!--         <h3><p>
            {{? value.category == 1}}
            <i>【商城商品】</i>
            {{??}}
            <i>【当月商品】</i>
            {{?}}
            订单号：{{=value.pay_number}}</p><span>{{=value.action.status_str}}</span></h3> -->

            <div class="shop-order-goods-t">
                <h3><i>
                    {{? value.category == 1}}
                    <i>【商城商品】</i>
                    {{??}}
                    <i>【当月商品】</i>
                    {{?}}
                </i> <span>{{=value.action.status_str}}</span></h3>
                <p>订单号：{{=value.pay_number}}</p>
            </div>
            <ul tapmode onclick="order_details({{=value.id}})">
                {{~value.cart:valuee:indexx}}
                {{? valuee.product}}
                <li>
                    <img src="{{=valuee.product.image}}">
                    <dl>
                        <dt>{{=valuee.product.title}}</dt>
                        <p>规格：{{=valuee.product.unit_title}}</p>
                        <dd>
                            ￥{{=valuee.price}}
                            <span>x{{=valuee.number}}</span>
                        </dd>
                    </dl>
                </li>
                {{??}}
                {{?}}
                {{~}}
            </ul>
            <div class="shop-order-goods-m">
                <p>
                    <span>共{{=value.product_number}}件</span>
                    <!-- 合计￥{{=value.price_total}} -->
                    合计
                    {{? value.category == 1}}
                    <i>{{=value.price_total}}赚石</i>
                    {{??}}
                    {{? value.pay_method == 1}}
                    <i>{{=value.price_total}}赚石</i>
                    {{??}}
                    <i>{{=value.price_icon}}金币</i>
                    {{?}}
                    {{?}}
                </p>
            </div>
            <div class="shop-order-goods-d">
                {{~value.action.action:valueee:indexxx}}
                {{? valueee == "1"}}
                <span class="on" onclick="payfor({{=value.id}},'{{=value.price_total}}')">付款</span>
                {{?? valueee == "2"}}
                <span class="on" onclick="service('{{=value.id}}','{{=value.pay_number}}');">换货</span>
                {{?? valueee == "4"}}
                <span class="on" onclick="logistics('{{=value.post_url}}');">查看物流</span>
                {{?? valueee == "7"}}
                <span class="on" onclick="handleOrder({{=value.id}},'delete');">删除订单</span>
                {{?? valueee == "8"}}
                <span class="on" onclick="handleOrder({{=value.id}},'cancel');">取消订单</span>       
                {{?? valueee == "9"}}
                <span class="on" onclick="handleOrder({{=value.id}},'receive');">确认收货</span>
                {{??}}
                {{?}}
                {{~}}
            </div>
        </div>
        {{~}}
    </script>
    <script type="text/javascript">

        apiready = function(e){

            type = api.pageParam.type;

            if (type == 1) {
                $('#title').text('待付款');
            }else if(type == 2){
                $('#title').text('待发货');
            }else if(type == 3){
                $('#title').text('待收货');
            }else if(type == 5){
                $('#title').text('已收货');
            }else if(type == 6){
                $('#title').text('换货');
            }else{
                $('#title').text('全部订单');
            }

        // changeChooseState(type);
        pullLoading();
        // pullRefresh();
        reloadListener(); 
        loadData(0,type);
    }

    function payfor(id,price){
        api.openWin({
            name: 'shop_order_pay_password',
            url: './shop_order_pay_password.html',
            pageParam: {
                order_id: id,
                price: price
            }
        });
    }

    function loadData(more,type){
        // 进度提示框
        api.showProgress({
            title: '努力加载中...',
            text: '请稍等...',
            modal: false
        });

        if (more) {
            page += 1;
        }else{
            page = 1;
        }

        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/order/index',
            method: 'get',
            data: {
                values: {
                    page: page,
                    row: row,
                    category: type,
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                // 关闭上拉加载控件
                api.hideProgress();
                // // 关闭下拉刷新控件
                // api.refreshHeaderLoadDone();
                if (ret.data) {

                    for (var i = 0; i < ret.data.length; i++) {
                        ret.data[i].action.action = ret.data[i].action.action.split(',');
                    }

                    if (more) {
                        templateFun(ret.data,1);
                    }else{
                        templateFun(ret.data);
                    }
                }else{
                    if (more) {
                        templateFun(ret.data,1);
                    }else{
                        templateFun(ret.data);
                    }
                }
            } else {
                // alert( JSON.stringify( err ) );
                // 关闭上拉加载控件
                api.hideProgress();
                // 关闭下拉刷新控件
                // api.refreshHeaderLoadDone();
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(data,more){
                // alert( JSON.stringify( data ) );
                var template = $api.byId('template');
                var dotFn = doT.template(template.innerHTML);
                var html = dotFn(data);
                var list = $api.byId('list');

                if (more) {

                    if (data.length) {

                        $api.append(list, html);
                    }else{

                     api.toast({
                      msg: '没有更多了',
                      duration: 2000,
                      location: 'bottom'
                  });
                     return; 
                 }
             }else{
                if (data.length) {
                 $api.html(list, html);
             }else{
              var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:0.5rem;">暂无内容</h2>';
              list.innerHTML = html;
          }
      }
  }

    // 处理订单
    function handleOrder(id,action){

        var msg = '';
        if (action == 'delete') {
            msg = '是否删除订单?';
        }else if(action == 'cancel'){
            msg = '是否取消订单?';
        }else if(action == 'receive'){
            msg = '是否确认收货?';
        }

        api.confirm({
            title: '提示!',
            msg: msg,
            buttons: ['确定', '取消']
        }, function(ret, err){
            if( ret ){
             // alert( JSON.stringify( ret ) );
             if (ret.buttonIndex == 1) {
                var token = $api.getStorage('token');
                api.ajax({
                    url: webAddress + '/api/order/'+id+'/'+action,
                    method: 'get',
                    data: {
                        values: {
                            id:id
                        },
                    },
                    headers: {
                        Authorization: 'Bearer ' + token,
                        Accept: 'application/json',
                    },
                },function(ret, err){
                    if (ret) {
                        // alert( JSON.stringify( ret ) );
                        if (ret.status_code == 200) {
                            api.toast({
                                msg: ret.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                            loadData(0,type);
                        }
                    } else {
                        // alert( JSON.stringify( err ) );
                        api.toast({
                            msg: err.body.message,
                            duration: 2000,
                            location: 'bottom'
                        });
                        return false;
                    }
                });
            }
        }else{
             // alert( JSON.stringify( err ) );
             api.toast({
                msg: err.body.message,
                duration: 2000,
                location: 'bottom'
            });
             return false;
         }
     });

    }

    // 换货
    function service(order_id,pay_number){
        api.openWin({
            name: 'replacement',
            url: './replacement.html',
            pageParam: {
                order_id: order_id,
                pay_number: pay_number,
            }
        });
    }

    // 查看物流
    function logistics(post_url){
        api.openWin({
            name: 'logistics',
            url: './web_view.html',
            pageParam: {
                url: post_url,
                title: '查看物流'
            }
        });
    }

    // 上拉加载
    function pullLoading(){
        api.addEventListener({
          name: 'scrolltobottom',
          extra: {
            threshold: 0
        }
    }, function(ret, err){
        loadData(1,type);
    });
    }

    // 下拉刷新
    function pullRefresh(){
        api.setRefreshHeaderInfo({
            // loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function(ret, err) {
            //在这里从服务器加载数据，加载完成后调用api.refreshHeaderLoadDone()方法恢复组件到默认状态
            loadData(0,type);
        });
    }

    // 监听事件 (页面刷新)
    function reloadListener(){
        api.addEventListener({
            name: 'reload',
        }, function(ret, err){
           loadData(0,type);
       });
    }

    // 订单详情
    function order_details(id) {
        api.openWin({
            name: 'order_details',
            url: './order_details.html',
            pageParam:{
                id: id,
            }
        });
    }

    function closeWin(){
        var setSele = 'gotoUserIndex();';
        api.execScript({
            name: 'root',
            script: setSele,
        });
        api.closeToWin({
            name: 'root'
        });
    }
</script>
</html>